package cn.jee.config;

import cn.jee.interceptor.SecInterceptor;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.sql.SQLException;
import java.util.Locale;

@Configuration
public class SelfWebMcv implements WebMvcConfigurer, ErrorPageRegistrar {
  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor(){
    LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    return localeChangeInterceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
    registry.addInterceptor(new SecInterceptor()).addPathPatterns("/sec/**");
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    WebMvcConfigurer.super.addViewControllers(registry);
    registry.addViewController("/sec/private").setViewName("/private");
    registry.addViewController("/error/sql_ex").setViewName("/error/sql_ex");
  }

  //语言和地区的解析器
//  @Bean
//  public SessionLocaleResolver localeResolver(){
//    SessionLocaleResolver sessionLocaleResolver=new SessionLocaleResolver();
//    sessionLocaleResolver.setDefaultLocale(Locale.CHINESE);
//    return sessionLocaleResolver;
//  }
  @Bean
  public CookieLocaleResolver localeResolver(){
    CookieLocaleResolver resolver=new CookieLocaleResolver();
    resolver.setDefaultLocale(Locale.CHINESE);
    return  resolver;
  }

  @Override
  public void registerErrorPages(ErrorPageRegistry registry) {
    registry.addErrorPages(new ErrorPage(SQLException.class,"/error/sql_ex"));
  }
}
